const app = getApp();
const api = require('../../utils/api');

Page({
    data: {
        departmentId: '',
        categoryId: '',
        categoryName: '',
        keywords: '',
        isFocus: false, //搜索框聚焦
        showSearchResult: false,

        productList: [], //商品列表
        pageNum: 1,
        pageSize: 10,
        total: 0,
        noContent: false,
        isFinished: false, //加载结束
    },
    onLoad(options) {
       // this.selectComponent('#input').focus();
        this.setData({
            departmentId: options.departmentId ? options.departmentId : '',
            isFocus: true,
        });
    },
    onShow() {
        
    },
    searchProduct() { //搜索全部商品
        let params = {
            departmentId: this.data.departmentId,
            keyword: this.data.keywords,
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        }
        api.post(`/index/product/search`, params)
            .then(res => {
                let list = res.content;
                list.forEach(item => {
                    item.skuSaleList.forEach(pro => {
                        pro.price = pro.price.toFixed(2);
                    });
                });
                let newList = [...this.data.productList, ...list];
                this.setData({
                    productList: newList,
                    total: res.total,
                    showSearchResult: true,
                });

                //是否无内容标识
                if(this.data.productList.length == 0) {
                    this.setData({
                        noContent: true
                    })
                } else {
                    this.setData({
                        noContent: false
                    });
                }
                //是否加载完毕标识
                if(this.data.productList.length >= this.data.total) {
                    this.setData({
                        isFinished: true
                    });
                } else {
                    this.setData({
                        isFinished: false
                    });
                }
                wx.stopPullDownRefresh();
            })
    },

    //搜索商品
    bindInput(e) {
        let val = e.detail.value;
        this.setData({
            keywords: val
        });
    },
    clearKeywords() { //清除搜索关键字
        this.setData({
            keywords: '',
            isFocus: true
        });
    },
    submitSearch() { //提交搜索
        if(!this.data.keywords) {
            wx.showToast({
                title: '请输入搜索商品名称',
                icon: 'none'
            });
            return;
        }
        this.setData({
            productList: [],
            pageNum: 1,
            total: 0,
            isFinished: false,
        });

        this.searchProduct();
    },

    gotoStore(e) { //跳转店铺首页
        let item = e.currentTarget.dataset.item;
        wx.navigateTo({
            url: '/pages/storeCategory/storeCategory?id=' + item.shopId,
        });
    },
    viewProductDetail(e) { //查看商品详情
        let item = e.currentTarget.dataset.item;
        let index = e.currentTarget.dataset.index;
        wx.navigateTo({
            url: `/pages/storeCategoryProduct/storeCategoryProduct?id=${item.id}&shopId=${this.data.productList[index].shopId}`,
        });
    },

    onPullDownRefresh() {
        //下拉刷新
        this.setData({
            productList: [],
            pageNum: 1,
            total: 0,
            isFinished: false,
        });
        this.submitSearch();
    },
    onReachBottom() {
        //加载更多
        if(!this.data.isFinished) {
            this.setData({
                pageNum: this.data.pageNum+1,
            });
            
            setTimeout(() => {
                this.searchProduct();
            }, 800);
        }
    },
})